<template>
  <el-dialog
    v-loading="loading"
    class="assign-role"
    title="分配角色"
    :visible="showRoleDialog"
    @close="handleCloseDialog"
    @open="handleOpenDialog"
  >
    <!-- 这里准备复选框 -->
    <el-checkbox-group v-model="checkList">
      <!-- el-checkbox收集是label 文字描述写在双标签内部-->
      <el-checkbox v-for="item in rowslsit" :key="item.id" :label="item.id">{{
        item.name
      }}</el-checkbox>
    </el-checkbox-group>

    <template #footer>
      <el-button type="primary" size="small" @click="Clicksubmit">确定</el-button>
      <el-button size="small">取消</el-button>
    </template>
  </el-dialog>
</template>

<script>
import { reqGetRoleLise, reqAssignRoles } from '@/api/setting'
import { reqGetBaseInfo } from '@/api/user'
export default {
  props: {
    showRoleDialog: {
      type: Boolean,
      default: false
    },
    // 用户的id 用来查询当前用户的角色信息
    userId: {
      type: String,
      default: null
    }
  },
  data() {
    return {
      checkList: [], // 双向绑定进行数据回显
      rowslsit: [], // 员工列表
      loading: false
    }
  },
  methods: {
    //   打开对话框做的事情
    handleOpenDialog() {
      this.loading = true
      // 园区员工列表
      // 获取员工详情
      Promise.all([this.getRoleLise(), this.getRoleDeatil()]).then(() => {
        this.loading = false
      })
    },
    //   关闭弹窗
    handleCloseDialog() {
      this.$emit('update:showRoleDialog', false)
      // 情况员工列表
      this.checkList = []
    },
    // 园区员工列表
    async getRoleLise() {
      const {
        data: { rows }
      } = await reqGetRoleLise(1, Infinity)
      //   赋值给员工列表
      this.rowslsit = rows
    },
    // 获取员工详情
    async getRoleDeatil() {
      const { data: { roleIds }} = await reqGetBaseInfo(this.userId)
      this.checkList = roleIds
    },
    async  Clicksubmit() {
      const res = await reqAssignRoles(this.userId, this.checkList)
      this.$message.success(res.message)
      this.handleCloseDialog()
    }
  }
}
</script>
